package com.cn.xiaonuo.common.utils;

import com.cn.xiaonuo.common.constant.AgentConstant;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

//代理工具类
public class AgentUtils {

    public static void main(String[] args){
        //System.out.println(paymentStateToday(new Date()));
    }

    //获得本周期的起始时间，结束时间，是否是结算日期
    public static Map<String, Object> queryCycleTime(){
        //今日时间
        Date todayDate = new Date();
        //Date todayDate = new Date(1587864914000L);//伪造todayDate为某周期最后一天日期，注意Java时间戳是毫秒级别

        //准备返回参数
        Map<String, Object> maps = new HashMap<>();
        maps.put("paymentStateToday",false);//默认今日不是结算日期

        try {
            //获得今日是本周期的第几天
            long dayOfCycle = dayOfCycle(todayDate);

            //判断今日是否是结算日期
            Boolean paymentStateToday = paymentStateToday(todayDate);
            maps.put("paymentStateToday",paymentStateToday);

            //今天结束时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//日期格式
            String todayEndTime = sdf.format(todayDate) + " 23:59:59";//今日结束时间
            SimpleDateFormat sdfHms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式
            long todayEndMillis = sdfHms.parse(todayEndTime).getTime();//今日结束时间毫秒数

            //周期开始时间
            long startDayMillis = todayEndMillis-dayOfCycle*24*60*60*1000+1000;//周期开始时间毫秒数
            String startTime = sdfHms.format(new Date(startDayMillis));//周期开始时间

            //周期结束时间
            long endDayMillis = todayEndMillis+(AgentConstant.CYCLE_DAY-dayOfCycle)*24*60*60*1000;//周期结束时间毫秒数
            String endTime = sdfHms.format(new Date(endDayMillis));//周期结束时间

            maps.put("startTime",startTime);
            maps.put("endTime",endTime);
            return maps;
        } catch (Exception e) {
            e.printStackTrace();
            return maps;
        }
    }

    //今日是否是本周期结算时间。
    public static Boolean paymentStateToday(Date todayDate){
        //1.默认今日不是本周期结算时间
        Boolean paymentState = false;
        try {
            //2.今日是本周期的第几天
            long dayOfCycle = dayOfCycle(todayDate);

            //3.判断今日是否是本周期结算时间。
            if(dayOfCycle== AgentConstant.PAYMENT_CYCLE_DAY){
                paymentState = true;
            }
            return paymentState;
        } catch (Exception e) {
            e.printStackTrace();
            return paymentState;
        }
    }

    //今日是本周期的第几天。
    public static long dayOfCycle(Date todayDate){
        //1.今日是本周期第几天初始值
        long dayOfCycle = -1;
        try {
            //2.今天是起始天的第几天
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置转换的日期格式
            Date startDate = sdf.parse(AgentConstant.CYCLE_DATE);//开始时间
            Date endDate = sdf.parse(sdf.format(todayDate));//结束时间
            long betweenDate = (endDate.getTime() - startDate.getTime())/(60*60*24*1000)+1;

            //3.判断今日是否是本周期最后一天
            dayOfCycle = betweenDate%AgentConstant.CYCLE_DAY;
            if(dayOfCycle==0){
                dayOfCycle=AgentConstant.CYCLE_DAY;//刚好整除，那就是周期的最后一天
            }
            return dayOfCycle;
        } catch (Exception e) {
            e.printStackTrace();
            return dayOfCycle;
        }
    }

    public static  List<Map<String, String>> cycleTime(String times) {
        List<Map<String, String>> mapList = new ArrayList<>();
        try {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDate l1 = LocalDate.parse(times, dateTimeFormatter);
            LocalDate l2 = LocalDate.parse(AgentConstant.CYCLE_DATE_DETAIL, dateTimeFormatter);
            int days = (int) (l1.toEpochDay() - l2.toEpochDay());
            for (int i = 0; i <= days / AgentConstant.CYCLE_DAY; i++) {
                Map<String, String> map = new HashMap<>(16);
                ZoneId zone = ZoneId.systemDefault();
                Instant instant = l1.minusDays(AgentConstant.CYCLE_DAY * i).atStartOfDay().atZone(zone).toInstant();
                LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant, zone);
                map.put("begin",dateTimeFormatter.format(localDateTime1));
                instant=instant.plusMillis(14*24*60*60*1000).minusMillis(1000);
                LocalDateTime localDateTime2 = LocalDateTime.ofInstant(instant, zone);
                map.put("end",dateTimeFormatter.format(localDateTime2));
                mapList.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapList;
    }

}